#!/bin/bash

# Gitee Webhook自动部署设置脚本

set -e

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'

info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }
title() { echo -e "${BLUE}$1${NC}"; }

echo ""
title "=========================================="
title "  Gitee Webhook 自动部署设置"
title "=========================================="
echo ""

# 检查root权限
if [ "$EUID" -ne 0 ]; then 
    error "请使用root权限运行此脚本"
    echo "使用方法: sudo ./setup-webhook.sh"
    exit 1
fi

# 安装依赖
info "安装Python依赖..."
apt-get update
apt-get install -y python3 python3-pip

pip3 install flask gunicorn

# 创建日志目录
info "创建日志目录..."
mkdir -p /var/log
touch /var/log/aps-deploy.log
chmod 666 /var/log/aps-deploy.log

# 配置systemd服务
info "配置systemd服务..."
cp webhook-server.service /etc/systemd/system/

# 提示用户配置
echo ""
warn "=========================================="
warn "  重要：请配置以下内容"
warn "=========================================="
echo ""
echo "1. 编辑服务配置文件："
echo "   sudo vim /etc/systemd/system/webhook-server.service"
echo ""
echo "2. 修改以下环境变量："
echo "   - WEBHOOK_SECRET: Webhook密钥（在Gitee中配置）"
echo "   - DEPLOY_DIR: 部署目录路径"
echo "   - IMAGE_REGISTRY: 镜像仓库地址"
echo "   - IMAGE_NAMESPACE: 镜像命名空间"
echo ""

read -p "是否现在编辑配置文件? (y/n): " edit_config
if [ "$edit_config" == "y" ]; then
    vim /etc/systemd/system/webhook-server.service
fi

# 重载systemd
info "重载systemd配置..."
systemctl daemon-reload

# 启动服务
info "启动Webhook服务..."
systemctl start webhook-server
systemctl enable webhook-server

# 检查状态
sleep 2
if systemctl is-active --quiet webhook-server; then
    echo ""
    title "=========================================="
    title "  ✅ Webhook服务启动成功！"
    title "=========================================="
    echo ""
    
    # 获取服务器IP
    SERVER_IP=$(hostname -I | awk '{print $1}')
    
    info "服务信息："
    echo "  - 服务状态: 运行中"
    echo "  - Webhook URL: http://${SERVER_IP}:5000/webhook"
    echo "  - 健康检查: http://${SERVER_IP}:5000/health"
    echo ""
    
    info "查看日志："
    echo "  - 服务日志: sudo journalctl -u webhook-server -f"
    echo "  - 部署日志: tail -f /var/log/aps-deploy.log"
    echo ""
    
    info "服务管理命令："
    echo "  - 启动: sudo systemctl start webhook-server"
    echo "  - 停止: sudo systemctl stop webhook-server"
    echo "  - 重启: sudo systemctl restart webhook-server"
    echo "  - 状态: sudo systemctl status webhook-server"
    echo ""
    
    warn "=========================================="
    warn "  下一步：在Gitee中配置Webhook"
    warn "=========================================="
    echo ""
    echo "1. 进入Gitee仓库 -> 设置 -> WebHooks"
    echo "2. 添加WebHook："
    echo "   - URL: http://${SERVER_IP}:5000/webhook"
    echo "   - 密钥: 与服务配置中的WEBHOOK_SECRET一致"
    echo "   - 触发事件: 选择 'Tag Push'"
    echo "3. 保存并测试"
    echo ""
    
    info "配置防火墙（如果需要）："
    echo "  sudo ufw allow 5000/tcp"
    echo ""
    
else
    error "Webhook服务启动失败"
    echo "查看错误日志: sudo journalctl -u webhook-server -n 50"
    exit 1
fi

